#ifndef HEADER_BLOCK
#define HEADER_BLOCK

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#endif

#include "character.h"

#define DEBUG 1           //if 1, extra print statements executed
#define MAXNVAL 50        //max number of characters user is able to create

int main(int argc, char ** argv)
{
  struct input traits;   //to be passed to create function
  
  zeroInput(&traits);    //ensure initial values in struct are 0.
  if(DEBUG) printf("Inputs have been zeroed\n");
  
  //int hcflag;    //hair color flag (hair color arg received)
  //int ctflag;    //hair type flag
  //int rflag;     //race flag

  int c;           //used for getopt parsing
  int n;           //number of characters to be generated
  int temp;
  unsigned int i;

/*
 * ARG VALUES AND MEANING
 *     -c : user wants to set haircolor to certain value, accepts int
 *     -t : user wants to set hairtype to certain value, accepts int
 *     -r : user wants to set race to certain value, accepts int
 *     -n : user wants to create n characters, accepts int, defaults to 1.
 *     -p : user wants to give custom name to character, accepts string
 */  

  while ((c = getopt (argc, argv, "c:t:r:n:p:")) != -1)
    {
      switch (c)
	{
	case 'c':
	  if ( (temp = atoi(optarg)) >= HCMAX)
	    {
	      printf("ERROR: HAIR COLOR VALUES RANGE FROM 0 - %d. YOU INPUT %d.\n", (HCMAX - 1), temp);
	      exit(1);
	    }  
	  traits.hairColor = temp;
	  if(DEBUG)printf("Hair color set\n");
	  break;
	case 't':
	  if ( (temp = atoi(optarg)) >= HTMAX)
	    {
	      printf("ERROR: HAIR TYPE VALUES RANGE FROM 0 - %d. YOU INPUT %d.\n", (HTMAX - 1), temp);
	      exit(1);
	    }  
	  traits.hairType = temp;
	  if(DEBUG)printf("Hair type set\n");
	  break;
	case 'r':
	  if ( (temp = atoi(optarg)) >= RMAX)
	    {
	      printf("ERROR: RACE VALUES RANGE FROM 0 - %d. YOU INPUT %d.\n", (RMAX - 1), temp);
	      exit(1);
	    }  
	  traits.race = atoi(optarg);
	  if(DEBUG)printf("Race set\n");
	  break;
	case 'n':
	  n = atoi(optarg);
	  if ( n > MAXNVAL)
	    {
	      printf("ERROR: CAN ONLY CREATE %d CHARACTERS AT A TIME.\n", MAXNVAL);
	      exit(1);
	    }
	  break;
	case 'p':
	  traits.name = optarg;
	  break;
	case '?':
	  if (optopt == 'c')
	    fprintf (stderr, "Option -%c requires an argument.\n", optopt);
	  else if (isprint (optopt))
	    fprintf (stderr, "Unknown option `-%c'.\n", optopt);
	  else
	    fprintf (stderr,
		     "Unknown option character `\\x%x'.\n",
		     optopt);
	  return 1;
	default:
	  abort();
	  break;
	}

    }
  
  if (DEBUG)
    {
      printf("Hair color = %d\n", traits.hairColor);
      printf("Hair type = %d\n", traits.hairType);
      printf("Race = %d\n", traits.race);
      printf("Name = %s\n", traits.name);
    }

  character array[n];
  for (int i = 0; i < n; ++i)
    {
      array[i].initialize(&traits);
      array[i].print();
    }

  return 0;
}

